#ifdef MBM_IMIBASE_SUPPORT

//include file
#include "mbm_spreadtrum_inc.h"
#ifndef _MP4CODEC_SC6600L_
#define _MP4CODEC_SC6600L_
#endif
#include "types.h"
#include "mpeg4dec.h"
#include "isp_service.h"
#include "freq_cfg.h"
#include "lcd_cfg.h"
#include "dc_common_6600l.h"

#include "mbm_typedefine.h"
#include "mbm_sys_adapter.h"

#include "mbm_h263_dec_adapter.h"
#include "mbm_memory_adapter.h"

//#define MBM_H263_DEC_DEBUG

#ifdef MBM_H263_DEC_DEBUG
#define MBMDEC_TRACE mbm_trace
#else
#define MBMDEC_TRACE
#endif

#define MBM_DEC_BUFFER_ALIGN_BIT		8
#define MBM_DEC_BUFFER_ADDR_ALIGN		((1<<MBM_DEC_BUFFER_ALIGN_BIT) - 1)
#define MBM_RIGHT_TRUNK(x)  ((((x) + MBM_DEC_BUFFER_ADDR_ALIGN) >> MBM_DEC_BUFFER_ALIGN_BIT)<<MBM_DEC_BUFFER_ALIGN_BIT)

//extern variable

//extern function

//static or gloable variable

//static function

ImpBool mbm_init_decoder(UInt8* cach_frames_buf, UInt32 buf_len, UInt32 frame_bfr_num)
{
	MMDecRet ret = MMDEC_OK;
	MMDecBuffer initDecBuf = {0};
	MMDecVideoFormat initDecVidFmt = {0};

	initDecBuf.common_buffer_ptr = NULL;
	initDecBuf.size = 0;
	initDecBuf.frameBfr_num = frame_bfr_num;
	initDecBuf.int_buffer_ptr = 0x40002000;
	initDecBuf.int_size = 0x1000;

	initDecVidFmt.is_h263 = 1;
	initDecVidFmt.frame_width = 80;
	initDecVidFmt.frame_height = 64;
	initDecVidFmt.i_extra = 0;
	initDecVidFmt.p_extra = NULL;

	ret = MP4DecInit(&initDecBuf, &initDecVidFmt);
	if(ret != MMDEC_OK)
	{
		MBMDEC_TRACE("MP4DecInit fail ret=%d", ret);
		return FALSE;
	}

	initDecBuf.common_buffer_ptr = MBM_RIGHT_TRUNK((UInt32)cach_frames_buf);
	initDecBuf.size = buf_len;
	initDecBuf.frameBfr_num = frame_bfr_num;
	initDecBuf.int_buffer_ptr = NULL;
	initDecBuf.int_size = 0;
	ret = MP4DecMemInit(&initDecBuf);
	if(ret != MMDEC_OK)
	{
		MBMDEC_TRACE("MP4DecMemInit fail ret=%d", ret);
		return FALSE;
	}
	
	return TRUE;
}

Int32 mbm_decode(const Int8* input, Int32 bytes, Int32* availble, t_mbm_yuv_info* info)
{
	MMDecInput dec_input = {0};
	MMDecOutput dec_output = {0};
	MMDecRet dec_ret = MMDEC_OK;
	
	SCI_ASSERT(input != 0);

	MBMDEC_TRACE("mbm_decode bytes=%d", bytes);

	if(MPEG4DEC_VSP_Available() == FALSE)
	{
		MBMDEC_TRACE("mbm_decode MPEG4DEC_VSP_Available fail");
		*availble = 0;
		return 0;
	}
	dec_input.pStream = input;
	dec_input.dataLen = bytes;

	dec_ret = MP4DecDecode(&dec_input, &dec_output);
	if(dec_ret != MMDEC_OK)
	{
		MBMDEC_TRACE("MP4DecDecode fail ret=%d", dec_ret);
		*availble = 0;
		return 0;
	}
	
	info->width = dec_output.frame_width;
	info->height = dec_output.frame_height;
	info->yuv_size = MBM_RIGHT_TRUNK(dec_output.frame_width * dec_output.frame_height)
						+ (dec_output.frame_width * dec_output.frame_height + 1)/2;

	*availble = dec_output.frameEffective;
	
	return dec_input.dataLen;
}

UInt32 mbm_get_one_dsp_frame(UInt8** yuv_buf)
{
	MMDecOutput output = {0};
	UInt32 yuv_size = 0;
	
	mpeg4dec_GetOneDspFrm(&output, 0);

	if(0 == output.frameEffective)
	{
		MBMDEC_TRACE("Assert! mbm_decode_picture frameEffective == 0 the output.pOutFrameY=%x", output.pOutFrameY);
		*yuv_buf = NULL;
		return 0;
	}

	yuv_size = MBM_RIGHT_TRUNK(output.frame_width * output.frame_height)
						+ (output.frame_width * output.frame_height + 1)/2;

	*yuv_buf = output.pOutFrameY;
	
	MBMDEC_TRACE("mbm_decode_picture the output.pOutFrameY=%x", output.pOutFrameY);
	
	return yuv_size;
}

void mbm_dec_release(void)
{
	MP4DecRelease();
	MBMDEC_TRACE("mbm_decode_close");
}

#endif//MBM_IMIBASE_SUPPORT

